package com.jxsd.edu.cn.tools;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * 分页响应器
 * */
public class Page<T> 
{
	private   int        current_page=1;//当前页
	private   int        pageSize=4;//页面大小
	private   int        pageCount=1;//总页数
	private   int        rowCount=0;//总记录数
	private   boolean    isIndexPage=true;//是否首页
	private   boolean    isLastPage=false;//是否最一页
	private   int        previousPage;//上一页
	private   int        lastPage;//下一页
	private   List<T>    list;//页面数据
	
	private  static  Log  logger=LogFactory.getLog("DAO");
	 
	//获取当前页
	public int  getCurrent_page()
	{
		return  current_page;
	}
	//获取页面大小
	public  int  getPageSize()
	{
		return  pageSize;
	}
	public  void  setPageSize(int pageSize)
	{
		this.pageSize=pageSize;
	}
	//获取总页数
	public  int  getPageCount()
	{
		return pageCount;
	}
	/**
     * 设置总记录数
     * */
    public  void  setRowCount(String sql,Object[]  params)
    {
    	String  str="select count(*)  "+sql.substring(sql.indexOf("from"));
    	PreparedStatement  prestmt=null;
    	Connection conn=null;
    	//    	DbSource  tool=new DbSource();
    	
//    	for(Object  o:params)
//    	{
//    		System.out.println("$$$$$$$$$$$$$Page:params:"+o);
//    	}
    	System.out.println("count  SQL:"+str);
    	
    	try {
			conn=DbTool.getConnection();
			prestmt=conn.prepareStatement(str);
			
			if(params!=null)
			{
				for(int i=0;i<params.length;i++)
				{
					prestmt.setObject(i+1, params[i]);
				}
			}
			
			ResultSet  result=prestmt.executeQuery();
			
			//将指针移到结果集的最后一行
			result.last();
			//获取最后一行的索引
			int  num=result.getRow();
			if(num>1)
			{
				rowCount=num;
			}
			else
			{
				rowCount=result.getInt(1);
			}
			
			DbTool.close(prestmt, conn, result);
		} catch (SQLException e) {
		   e.printStackTrace();
		}
    	setPageCount();//设置总页数
    }
    
    public  int  getRowCount()
    {
    	return  rowCount;
    }
    public  int  getPreviousPage()
    {
    	return previousPage;
    }
    
    
    /**
     * 设置总页数
     * */
    public  void  setPageCount()
    {
    	if(rowCount==0)
    	{
    		pageCount=1;
    	}
    	else if(rowCount%pageSize==0) 
    	{
    	  pageCount=rowCount/pageSize;
    	  System.out.println("Page1:"+pageCount);
    	}
    	else 
    	{
    		pageCount=(rowCount/pageSize)+1;
    		System.out.println("Page2:"+pageCount);
    	}
    	
    	System.out.println("*******************************************");
    	System.out.println("Page  setPageCount");
    	System.out.println("*******************************************");
    	
    }
    
    /**
     * 设置当前页
     * */
    public  void  setCurrent_page(int  current)
    {
    	current_page=current;
    	if(current_page==1)
    	{
    		previousPage=1;
    		if(pageCount>1)
    		{
    			lastPage=current_page+1;
    		}
    		else
    		{
    			lastPage=current_page;
    		}
    	}
    	else if(current_page==pageCount)
    	{
    		previousPage=current_page-1;
    		lastPage=current_page;
    	}
    	else
    	{
    		previousPage=current_page-1;
    		lastPage=current_page+1;
    	}
    }
    /**
     * 是否尾页
     * */
    public  void  setIsLastPage()
    {
    	//最后一页
    	if(lastPage==pageCount)
    	{
    		isLastPage=true;
    	}
    	else 
    	{
    		isLastPage=false;
    	}
    }
    
    public  int  getLastPage()
    {
    	return lastPage;
    }
    /**
     * 设置是否首页
     * */
    public  void  setIsIndexPage()
    {
    	if(previousPage==1)
    	{
    		isIndexPage=true;
    	}
    	else
    	{
    		isIndexPage=false;
    	}
    }
	public List<T> getList() {
		return list;
	}
	public void setList(List<T> list) {
		this.list = list;
	}
    
}